package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.pojo.Classification;
import com.itheima.service.ClassificationService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 林晓伟
 */
@RestController
@RequestMapping("/category")
public class ClassificationController {
    @Resource
    private ClassificationService classificationService;

    /**
     * H07-新增分类
     */
    @PostMapping
    public R add(@RequestBody Classification classification){
        //判断分类是否重复
        Integer judge = classificationService.judge(classification.getName());
        if (judge != 0){
            return R.error("'分类名'已存在");
        }
        //添加分类
        Integer add = classificationService.add(classification);
        if (add != 0){
            return R.success("添加成功！");
        }
        return R.error("添加失败！请重试");
    }

    /**
     * H10-编辑分类
     */
     @PutMapping
    public R update(@RequestBody Classification classification){
        //修改分类
        Integer update = classificationService.update(classification);
        if (update != 0){
            return R.success("修改分类信息成功");
        }
        return R.success("修改分类信息失败");
    }

    /**
     * H09-删除分类
     */
    @DeleteMapping
    public R delete(@RequestParam Integer id){
        //根据id删除分类
        Integer delete = classificationService.delete(id);
        if (delete != 0){
            return R.success("删除成功！");
        }
        return R.error("删除失败！");
    }

    /**
     * H08-分类下拉列表
     * @param type 分类类别
     */
    @GetMapping("/list")
    public R list(@RequestParam String type){
        List<Classification> list = classificationService.list(type);
        return R.success(list);
    }

//    H08-分类信息分页查询

    @GetMapping("/page")
    public R select(@RequestParam Integer page,@RequestParam Integer pageSize){
        return R.success(classificationService.select(page,pageSize));
    }










}
